import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/fab_new_provider.dart';

import 'home_item_info.dart';

class HomeListPage extends StatefulWidget {
  final String index;
  bool show;

  HomeListPage(this.index, this.show);

  @override
  HomeListPageState createState() => HomeListPageState();
}

class HomeListPageState extends State<HomeListPage> {
  List<InvListData> dataList = List();
  int pos;
  bool load=true;

  @override
  void initState() {
    if (widget.index == "出库预约") {
      pos = 0;
      getData();
    } else if (widget.index == "配送预约") {
      pos = 1;
      getDeliveryData();
    } else if (widget.index == "结余退库") {
      pos = 2;
      getReturnData();
    } else if (widget.index == "入库预约") {
      pos = 3;
      getInData();
    } else if (widget.index == "调配预约") {
      pos = 4;
      getDepData();
    }

    WidgetsBinding.instance.addPostFrameCallback(
        (_) => Provider.of<FabNewProvider>(context).addListener(() {
              int position = Provider.of<FabNewProvider>(context).index;
              if (position == 0) {
                getData();
              } else if (position == 1) {
                getDeliveryData();
              } else if (position == 2) {
                getReturnData();
              } else if (position == 3) {
                getInData();
              } else if (position == 4) {
                getDepData();
              }
            }));
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      child: LoadBuilder(
        visibility: load,
        emptyVisibility: !load&&dataList.length==0,
        emptyTextVisibility: !load&&dataList.length==0,
        textEmptyTips: "暂无内容",
        builder:ListView.separated(
          itemCount: dataList.length,
          padding: EdgeInsets.only(top: 0),
          itemBuilder: (context, index) {
            if (pos == 0) {
              return HomeItemInfo(dataList[index], pos, getData);
            } else if (pos == 1) {
              return HomeItemInfo(dataList[index], pos, getDeliveryData);
            } else if (pos == 2) {
              return HomeItemInfo(dataList[index], pos, getReturnData);
            } else if (pos == 3) {
              return HomeItemInfo(dataList[index], pos, getInData);
            } else {
              return HomeItemInfo(dataList[index], pos, getDepData);
            }
          },
          separatorBuilder: (context, index) {
            return Divider(height: 2, color: Color(0xffeeeeee));
          },
        ),),

      onRefresh: _onRefresh,
    );
  }

  Future<void> _onRefresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    if (pos == 0) {
      getData();
    } else if (pos == 1) {
      getDeliveryData();
    } else if (pos == 2) {
      getReturnData();
    } else if (pos == 3) {
      getInData();
    } else if (pos == 4) {
      getDepData();
    }
  }

  void getData() {
    setState(() {
      load=true;
    });
    dataList.clear();
    InternetAPI.getInstance()
        .get("/OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask", (data) {
      if (widget.show) {
        WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
              load=false;
              dataList.addAll(InvListEntity.fromJson(data).datas);
            }));
      } else {
        setState(() {
          load=false;
          dataList.addAll(InvListEntity.fromJson(data).datas);
        });
      }
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "Warehouseouttype": "10",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getDeliveryData() {
    setState(() {
      load=true;
    });
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask", (data) {
      if (widget.show) {
        WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
          load=false;
              dataList.addAll(InvListEntity.fromJson(data).datas);
            }));
      } else {
        setState(() {
          load=false;
          dataList.addAll(InvListEntity.fromJson(data).datas);
        });
      }
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "Warehouseouttype": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getReturnData() {
    setState(() {
      load=true;
    });
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiBackWarehouseTask/GetAllMiBackWarehouseTask", (data) {
      if (widget.show) {
        WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
          load=false;
              dataList.addAll(InvListEntity.fromJson(data).datas);
            }));
      } else {
        setState(() {
          load=false;
          dataList.addAll(InvListEntity.fromJson(data).datas);
        });
      }
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "userid": DataSingleton().Data.id,
      "Warehouseouttype": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getInData() {
    setState(() {
      load=true;
    });
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseInTask/GetAllMiWarehouseInTask", (data) {
      if (widget.show) {
        WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
          load=false;
              dataList.addAll(InvListEntity.fromJson(data).datas);
            }));
      } else {
        setState(() {
          load=false;
          dataList.addAll(InvListEntity.fromJson(data).datas);
        });
      }
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "appointmentmode": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getDepData() {
    setState(() {
      load=true;
    });
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiAllocationTaskDetail/GetAllMiAllocationTaskDetail", (data) {
      if (widget.show) {
        WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
          load=false;
              dataList.addAll(InvListEntity.fromJson(data).datas);
            }));
      } else {
        setState(() {
          load=false;
          dataList.addAll(InvListEntity.fromJson(data).datas);
        });
      }
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "pageNo": 1,
      "pageSize": 5
    });
  }
}
